<template>
  <div class="rightbox" v-cloak>
    <h1>修改密码</h1>
    <div class="add-box" style="width:650px;">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item
          prop="phone"
          label="登录手机号"
          :rules="[
            { required: true, message: '请填写登录手机号', trigger: 'blur' }
          ]"
        >
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item
          prop="password"
          label="原始密码"
          :rules="[
            { required: true, message: '请填写原始密码', trigger: 'blur' }
          ]"
        >
          <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item
          prop="passwordone"
          label="用户密码"
        >
          <el-input type="password" v-model="form.passwordone"></el-input>
        </el-form-item>
        <el-form-item
          prop="passwordtwo"
          label="确认密码"
        >
          <el-input type="password" v-model="form.passwordtwo"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submit('form')">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    var password=/^(?![0-9]+$)(?![a-z]+$)(?![A-Z]+$)(?![,\.#%'\+\*\-:;^_`]+$)[,\.#%'\+\*\-:;^_`0-9A-Za-z]{8,}$/
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'))
      } else if (!password.test(value)) {
        callback(new Error('请设置至少包含两种字符的8位及以上密码'))
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.form.passwordone) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      form: {
        phone: '',
        password: '',
        passwordone: '',
        passwordtwo: ''
      },
      rules: {
        passwordone: [
          { validator: validatePass, trigger: 'blur' }
        ],
        passwordtwo: [
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit: function (form) {
      var that = this
      that.$refs[form].validate((valid) => {
        if (valid) {
          that.$axios.request('post', '/Account/ChangePassWord', {
            Phone: that.form.phone,
            Password: that.form.password,
            Passwordone: that.form.passwordone,
            Passwordtwo: that.form.passwordtwo
          })
            .then((res) => {
              if (res.code === 0) {
                that.$axios.alert(res.data, '提示', function () {
                  that.form.password = ''
                  that.form.passwordone = ''
                  that.form.passwordtwo = ''
                })
              }
            })
        } else {
          return false
        }
      })
    }
  },
  mounted () {
  }
}
</script>
